import { Module } from 'vuex'

export interface BreadcrumbState {
  activePath: string;
  pathOptions: Array<{ name: string; path: string }>
}

// eslint-disable-next-line
const breadcrumb:Module<BreadcrumbState, any> = {
  namespaced: true,
  state: () => ({
    activePath: '',
    pathOptions: []
  }),
  getters: {
    getActivePath (state) {
      return state.activePath
    },
    getPathOptions (state) {
      return state.pathOptions
    }
  },
  mutations: {
    SET_ACTIVE_PATH (state, payload) {
      state.activePath = payload
    },
    SET_PATH_OPTIONS (state, payload) {
      state.pathOptions = payload
    }
  },
  actions: {
    setPathOptions ({ commit }, data) {
      if (Array.isArray(data)) {
        const current = data[data.length - 1]
        commit('SET_ACTIVE_PATH', current?.path)
        commit('SET_PATH_OPTIONS', data)
      } else {
        commit('SET_ACTIVE_PATH', '')
        commit('SET_PATH_OPTIONS', [])
      }
    }
  }
}

export default breadcrumb
